//
//  BigImage.m
//  Store041
//
//  Created by 041 on 14-8-9.
//  Copyright (c) 2014年 panquan/liang. All rights reserved.
//

#import "BigImages.h"

@implementation BigImages

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        [self addSubview:button];
    }
    return self;
}

- (UIImage *)getBigImage:(NSString *)imageUrl
{
    image = [UIImage imageNamed:@"touxiang.png"];
    self.image = image;
    
    
    
    //    [[SDWebImageManager shareddManager]downloadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageLowPriority progress:^(NSUInteger receivedSize, long long expectedSize) {
    //
    //    } completed:^(UIImage *image2, NSData *data, NSError *error, BOOL finished) {
    //        self.image = image2;//图片
    //        image = image2;
    //
    //        [button addTarget:self action:@selector(tapAction) forControlEvents:UIControlEventTouchUpInside];
    //
    //    }];
    
    [[SDWebImageManager sharedManager]downloadWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageLowPriority progress:^(NSUInteger receivedSize, long long expectedSize) {
        
    } completed:^(UIImage *image2, NSError *error, SDImageCacheType cacheType, BOOL finished) {
        
        self.image = image2;//图片
        image = image2;
        
        //        [button addTarget:self action:@selector(tapAction) forControlEvents:UIControlEventTouchUpInside];
    }];
    return image;
}

//得到root viewControl
- (UIViewController *)appRootViewController
{
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}

- (void)tapAction {
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    scroll.backgroundColor = [UIColor blackColor];
    [[self appRootViewController].view addSubview:scroll];
    
    UIImageView *imagev = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,ScreenWidth*image.size.height/image.size.width)];
    imagev.image = image;
    if (imagev.frame.size.height < ScreenHeight) {
        imagev.center = scroll.center;
    }
    
    [scroll addSubview:imagev];
    scroll.contentSize = CGSizeMake(ScreenWidth, imagev.frame.size.height);
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction2)];
    [scroll addGestureRecognizer:tap];
    scroll.tag = 100;
}

- (void)tapAction2 {
    UIScrollView *scroll = (UIScrollView *)[[self appRootViewController].view viewWithTag:100];
    [scroll removeFromSuperview];
}

@end
